{% extends "base/micro.html" %}

{% block title %}
Service Update | SwarmOps - Docker管理平台
{% endblock %}

{% block container %}
<form class="form-horizontal" role="form" id="UpdateServiceForm">

  <div class="form-group">
    <label for="UpdateServiceName" class="col-sm-12">Service Name</label>
    <div class="col-sm-12">
      <input type="text" class="form-control" id="UpdateServiceName" name="name" placeholder="服务标识">
    </div>
  </div>

  <input type="hidden" class="form-control" id="UpdateServiceId" name="serviceId">
  <input type="hidden" class="form-control" id="UpdateServiceType" name="UpdateServiceType" value="ui">

  <div class="form-group">
    <label for="UpdateServiceImage" class="col-sm-12">Service Image&nbsp;<span class="glyphicon glyphicon-star-empty"></span></label>
    <div class="col-sm-12">
      <input type="text" class="form-control" id="UpdateServiceImage" name="image" placeholder="服务镜像">
    </div>
  </div>

  <div class="form-group">
    <label for="UpdateServicereplicas" class="col-sm-12">Service Replicas&nbsp;<span class="glyphicon glyphicon-star-empty"></span></label>
    <div class="col-sm-12">
      <input type="number" class="form-control" id="UpdateServicereplicas" name="replicas" placeholder="服务副本(实例数), 默认值为1">
    </div>
  </div>

  <div class="form-group">
    <label for="UpdateServiceEnv" class="col-sm-12">Service Env</label>
    <div class="col-sm-12">
      <input type="text" class="form-control" id="UpdateServiceEnv" name="env" placeholder='服务环境变量, 格式是"key=value", 多个挂载以逗号分隔'>
    </div>
  </div>

  <div class="form-group">
    <label for="UpdateServiceMount" class="col-sm-12">Service Mount</label>
    <div class="col-sm-12">
      <input type="text" class="form-control" id="UpdateServiceMount" name="mount" placeholder='服务挂载, 格式是"src:target:true|false:mode", true|false代表只读、读写, mode固定值bind, 多个挂载以逗号分隔'>
    </div>
  </div>

  <div class="form-group">
    <label for="UpdateServicePublish" class="col-sm-12">Service Publish</label>
    <div class="col-sm-12">
      <input type="text" class="form-control" id="UpdateServicePublish" name="publish" placeholder='服务发现(端口映射), 格式是"src:target:protocol", protocol默认值tcp, 多个映射以逗号分隔'>
    </div>
  </div>

  <div class="form-group">
    <label for="UpdateServiceDelay" class="col-sm-12">Service Delay</label>
    <div class="col-sm-12">
      <input type="number" class="form-control" id="UpdateServiceDelay" name="delay" value=10 placeholder='服务更新之间的延迟秒数, 默认为10'>
    </div>
  </div>

  <div class="form-group">
    <label for="UpdateServiceParallelism" class="col-sm-12">Service Parallelism</label>
    <div class="col-sm-12">
      <input type="number" class="form-control" id="UpdateServiceParallelism" name="parallelism" value=1 placeholder='同时更新的任务数, 默认为1'>
    </div>
  </div>

  <div class="form-group">
    <div class=" col-sm-12">
      <button type="button" onclick=UpdateService() class="btn btn-info">更新服务</button>
    </div>
  </div>

</form>

{% endblock %}
{% block script %}
<script type="text/javascript">
$(document).ready(function () {
    var serviceId = "{{ request.args.get('serviceId') }}";
    $.ajax({
        url: "{{ url_for('apis.core.service') }}?core=true&core_convert=true&id=" + serviceId,
        method: 'GET',
        datatype: 'json',
        success: function(res) {
            console.log(res);
            service = res.data[0];
            document.getElementById("UpdateServiceName").value = service.Name;
            document.getElementById("UpdateServiceId").value = service.ID;
            document.getElementById("UpdateServiceImage").value = service.Image;
            document.getElementById("UpdateServicereplicas").value = service.Replicas;
            document.getElementById("UpdateServiceEnv").value = service.Env;
            document.getElementById("UpdateServiceMount").value = service.Mounts;
            document.getElementById("UpdateServicePublish").value = service.NetPorts;
        },
    });
});
function UpdateService() {
    $.ajax({
        url: "{{ url_for('apis.core.service') }}",
        method: 'PUT',
        datatype: 'json',
        data: $('#UpdateServiceForm').serialize(),
        success: function(res) {
            console.log(res);
            if (res.success==true) {
                alert("更新服务成功!");
                //document.location.reload(); //重载当前页面
                parent.location.reload();
            } else {
                alert("更新服务失败，请重试提交或报告错误！\n错误原因是：" + res.msg);
            }
        },
        error: function(res) {
            console.log(res);
            alert(res.statusText);
        }
    });
}
</script>
{% endblock %}
